/**
 ** This file is part of FK Klistret Extensions. FK Klistret Extensions 
 ** is free software: you can redistribute it and/or modify it under the 
 ** terms of the GNU General Public License as published by the Free 
 ** Software Foundation, either version 3 of the License, or (at your option) 
 ** any later version.

 ** FK Klistret Extensions is distributed in the hope that it will be 
 ** useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 ** General Public License for more details. You should have received a
 ** copy of the GNU General Public License along with FK Klistret Extensions. 
 ** If not, see <http://www.gnu.org/licenses/>
 */
package test.se.fk.klistret.console.service;

import java.io.StringReader;
import java.net.URLEncoder;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;

import org.jboss.resteasy.client.ClientRequest;
import org.jboss.resteasy.client.ClientResponse;
import org.jboss.resteasy.plugins.providers.jaxb.JaxbCollection;
import org.junit.Before;
import org.junit.Test;
import org.w3c.dom.Node;

import com.klistret.cmdb.ci.element.context.Environment;
import com.klistret.cmdb.ci.pojo.Element;

import se.fk.klistret.console.jaxb.CIContext;

/**
 * 
 * @author Matthew Young
 *
 */
public class Experimenting {

	@Before
	public void setUp() throws Exception {
	}

	// @Test
	public void get() throws Exception {
		ClientRequest request = new ClientRequest(
				"http://sadbmatrix2:55167/CMDB/resteasy/element/{id}");

		request.accept("application/xml").pathParameter("id", 285);
		ClientResponse<String> response = request.get(String.class);

		if (response.getStatus() == 200) {
			System.out.println(response.getEntity());

			StreamSource source = new StreamSource(new StringReader(response
					.getEntity()));

			JAXBContext jaxbContext = CIContext.getCIContext().getJAXBContext();
			Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

			Element element = (Element) unmarshaller.unmarshal(source);
			System.out.println(((Environment) element.getConfiguration())
					.getName());
		}
	}

	@Test
	public void find() throws Exception {
		ClientRequest request = new ClientRequest(
				"http://sadbmatrix2:55167/CMDB/resteasy/element?expressions="
						+ URLEncoder
								.encode(
										"declare namespace pojo=\"http://www.klistret.com/cmdb/ci/pojo\"; /pojo:Element[empty(pojo:toTimeStamp)]/pojo:type[pojo:name eq \"{http://www.klistret.com/cmdb/ci/element/component/software}ApplicationSoftware\"]",
										"UTF-8")
						+ "&expressions="
						+ URLEncoder
								.encode(
										"declare namespace pojo=\"http://www.klistret.com/cmdb/ci/pojo\"; declare namespace software=\"http://www.klistret.com/cmdb/ci/element/component/software\"; /pojo:Element[empty(pojo:toTimeStamp)]/pojo:configuration[software:Type ne \"Version\"]",
										"UTF-8")
						+ "&expressions="
						+ URLEncoder
								.encode(
										"declare namespace pojo=\"http://www.klistret.com/cmdb/ci/pojo\"; /pojo:Element/pojo:destinationRelations[empty(pojo:toTimeStamp)]/pojo:source[pojo:name eq \"KUI\"]",
										"UTF-8"));

		request.accept("application/xml");
		ClientResponse<String> response = request.get(String.class);
		if (response.getStatus() == 200) {
			System.out.println(response.getEntity());

			StreamSource source = new StreamSource(new StringReader(response
					.getEntity()));

			JAXBContext jaxbContext = CIContext.getCIContext().getJAXBContext();
			Unmarshaller um = jaxbContext.createUnmarshaller();
			JAXBElement<JaxbCollection> collection = um.unmarshal(source,
					JaxbCollection.class);

			List<Object> results = collection.getValue().getValue();
			for (Object value : results) {
				Element element = (Element) um.unmarshal((Node) value);
				System.out.println(element.getName());
			}
		}
	}

	// @Test
	public void getCIContext() throws Exception, JAXBException {
		JAXBContext jaxbContext = CIContext.getCIContext().getJAXBContext();
		jaxbContext.createMarshaller();
	}
}
